package cn.stylefeng.guns.modular.business.timer;

import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.modular.business.custom.enums.OrderAbnormalTypeEnum;
import cn.stylefeng.guns.modular.business.custom.enums.OrderStatusEnum;
import cn.stylefeng.guns.modular.business.frequency.banner.config.Application;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.entity.OrderAbnormal;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.mapper.OrderAbnormalMapper;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.param.OrderAbnormalDTO;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.result.OrderAbnormalVO;
import cn.stylefeng.guns.modular.business.frequency.orderabnormal.model.result.PkgOrderAbnormal;
import cn.stylefeng.guns.modular.business.frequency.orderpick.mapper.OrderPickMapper;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgOrderSign;
import cn.stylefeng.guns.modular.business.frequency.ordersub.model.result.PkgPick;
import cn.stylefeng.guns.modular.business.frequency.syssite.entity.SysSite;
import cn.stylefeng.guns.modular.business.frequency.syssite.mapper.SysSiteMapper;
import cn.stylefeng.guns.modular.business.frequency.transportsign.mapper.TransportSignMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
// @Component
public class TimerThreadOne {


    @Autowired
    private OrderAbnormalMapper orderAbnormalMapper;
    @Autowired
    private SysSiteMapper siteMapper;
    @Autowired
    private OrderPickMapper pickCarMapper;
    @Autowired
    private TransportSignMapper orderSignMapper;


    /**
     * 每5分钟执行一次
     */
    // @Scheduled(cron = "0 */5 * * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void orderAbnormal() {
        log.info("开始执行异常订单扫描任务---------------------------------");

        List<OrderAbnormalVO> list = new ArrayList<>();
        // 提车超时 所有待指派结算人状态的订单
        List<OrderAbnormalVO> waitPickList = orderAbnormalMapper.getOrderAbnormalVOListByOrderStatus(OrderStatusEnum.NEW_ORDER.status);
        if (waitPickList != null && waitPickList.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : waitPickList) {
                if (System.currentTimeMillis() - orderAbnormalVO.getCreateTime().getTime() > Application.ORDER_PICK_OVER_TIME) {
                    // 异常类型
                    orderAbnormalVO.setType(1);
                    // 计算超时时间
                    orderAbnormalVO.setOverTime(dealOverTime(orderAbnormalVO.getCreateTime().getTime(), Application.ORDER_PICK_OVER_TIME));
                    // 责任人
                    List<SysSite> siteList = siteMapper.getListByChargeCode(orderAbnormalVO.getStartCityCode(),orderAbnormalVO.getTenantId());
                    // 出发地是否有网点负责
                    if (siteList != null && siteList.size() > 0) {
                        // 有网点
                        Long chargeSiteUserId = siteMapper.getIdBySiteId(siteList.get(0).getId(),orderAbnormalVO.getTenantId());
                        if (chargeSiteUserId==null) {
                            orderAbnormalVO.setChargeUser(chargeSiteUserId);
                        } else {
                            orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                        }
                    } else {
                        // 无网点
                        orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                    }
                    if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 1, null) == 0) {
                        list.add(orderAbnormalVO);
                    }
                }
            }
        }
        // 验车异常
        List<OrderAbnormalVO> verifyOrderList = orderAbnormalMapper.getOrderAbnormalVOListByOrderStatus(OrderStatusEnum.WAIT_PICK.status);
        //List<OrderAbnormalVO> verifyOrderList = orderSubMapper.getPkgOrderSubListByStatus(OrderStatusEnum.WAIT_PICK.status);
        if (verifyOrderList != null && verifyOrderList.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : verifyOrderList) {
                PkgPick pkgPick = pickCarMapper.getPkgPickDetail(orderAbnormalVO.getOrderId());
                if (pkgPick != null && pkgPick.getCreateTime() != null) {
                    // 有提车
                    if ((System.currentTimeMillis() - pkgPick.getCreateTime().getTime()) > Application.ORDER_VERIFY_OVER_TIME) {
                        orderAbnormalVO.setType(2);
                        orderAbnormalVO.setOverTime(dealOverTime(pkgPick.getCreateTime().getTime(), Application.ORDER_VERIFY_OVER_TIME));
                        // 责任人
                        List<SysSite> siteList = siteMapper.getListByChargeCode(orderAbnormalVO.getStartCityCode(), orderAbnormalVO.getTenantId());
                        if (siteList != null && siteList.size() > 0) {
                            Long chargeSiteUserId = siteMapper.getIdBySiteId(siteList.get(0).getId(), orderAbnormalVO.getTenantId());
                            if (chargeSiteUserId==null) {
                                orderAbnormalVO.setChargeUser(chargeSiteUserId);
                            } else {
                                orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                            }
                        } else {
                            // 无网点
                            orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                        }
                        if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 2, null) == 0) {
                            list.add(orderAbnormalVO);
                        }
                    }
                } else {
                    // 无提车
                    if ((System.currentTimeMillis() - orderAbnormalVO.getCreateTime().getTime()) > Application.ORDER_VERIFY_OVER_TIME) {
                        orderAbnormalVO.setType(2);
                        orderAbnormalVO.setOverTime(dealOverTime(orderAbnormalVO.getCreateTime().getTime(), Application.ORDER_VERIFY_OVER_TIME));
                        // 责任人
                        List<SysSite> siteList = siteMapper.getListByChargeCode(orderAbnormalVO.getStartCityCode(), orderAbnormalVO.getTenantId());
                        if (siteList != null && siteList.size() > 0) {
                            Long chargeSiteUserId = siteMapper.getIdBySiteId(siteList.get(0).getId(), orderAbnormalVO.getTenantId());
                            if (chargeSiteUserId==null) {
                                orderAbnormalVO.setChargeUser(chargeSiteUserId);
                            } else {
                                orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                            }
                        } else {
                            // 无网点
                            orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                        }
                        if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 2, null) == 0) {
                            list.add(orderAbnormalVO);
                        }
                    }
                }
            }
        }
        // 发运异常
        List<OrderAbnormalVO> transportList = orderAbnormalMapper.getOrderAbnormalVOListByOrderStatus(OrderStatusEnum.NOT_DEPART_ORDER.status);
        if (transportList != null && transportList.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : transportList) {
                if (System.currentTimeMillis() - orderAbnormalVO.getCreateTime().getTime() > Application.ORDER_TRANSPORT_OVER_TIME) {
                    // 异常类型
                    orderAbnormalVO.setType(3);
                    orderAbnormalVO.setOverTime(dealOverTime(orderAbnormalVO.getCreateTime().getTime(), Application.ORDER_TRANSPORT_OVER_TIME));
                    // 责任人
                    List<SysSite> siteList = siteMapper.getListByChargeCode(orderAbnormalVO.getStartCityCode(), orderAbnormalVO.getTenantId());
                    if (siteList != null && siteList.size() > 0) {
                        Long chargeSiteUserId = siteMapper.getIdBySiteId(siteList.get(0).getId(), orderAbnormalVO.getTenantId());
                        if (chargeSiteUserId==null) {
                            orderAbnormalVO.setChargeUser(chargeSiteUserId);
                        } else {
                            orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                        }

                    } else {
                        // 无网点
                        orderAbnormalVO.setChargeUser(orderAbnormalVO.getCreateUser());
                    }
                    if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 3, null) == 0) {
                        list.add(orderAbnormalVO);
                    }
                }
            }
        }
        // 中转发运异常
        List<OrderAbnormalVO> transferList = orderAbnormalMapper.getOrderAbnormalVOListByOrderStatus(OrderStatusEnum.TRANSFERRING_SIGN.status);
        if (transferList != null && transferList.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : transferList) {
                // 最后一次签收
                PkgOrderSign orderSign = orderSignMapper.getLastSignInfoByOrderId(orderAbnormalVO.getOrderId());

                if (orderSign!=null&&System.currentTimeMillis() - orderSign.getCreateTime().getTime() > Application.ORDER_TRANSPORT_OVER_TIME) {
                    // 异常类型
                    orderAbnormalVO.setType(5);
                    orderAbnormalVO.setOverTime(dealOverTime(orderSign.getCreateTime().getTime(), Application.ORDER_TRANSPORT_OVER_TIME));
                    orderAbnormalVO.setTransportId(orderSign.getTransportId());
                    // 责任人
                    orderAbnormalVO.setChargeUser(orderSign.getCreateUser());
                    if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 5, orderAbnormalVO.getTransportId()) == 0) {
                        list.add(orderAbnormalVO);
                    }
                }
            }
        }
        // 交付异常
        List<OrderAbnormalVO> deliveryList = orderAbnormalMapper.getOrderAbnormalVOListByOrderStatus(OrderStatusEnum.WAIT_TAKE.status);
        if (deliveryList != null && deliveryList.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : deliveryList) {
                // 有到达签收的
                PkgOrderSign orderSign = orderSignMapper.getDeliverySignByOrderId(orderAbnormalVO.getOrderId());
                if (orderSign != null) {
                    if ((System.currentTimeMillis() - orderSign.getCreateTime().getTime()) > Application.ORDER_DELIVERY_OVER_TIME) {
                        orderAbnormalVO.setType(4);
                        orderAbnormalVO.setChargeUser(orderSign.getCreateUser());
                        orderAbnormalVO.setOverTime(dealOverTime(orderSign.getCreateTime().getTime(), Application.ORDER_DELIVERY_OVER_TIME));
                        if (orderAbnormalMapper.countByOrderIdAndType(orderAbnormalVO.getOrderId(), 4, null) == 0) {
                            list.add(orderAbnormalVO);
                        }
                    }
                }
            }
        }
        // 异常订单入库
        if (list.size() > 0) {
            for (OrderAbnormalVO orderAbnormalVO : list) {
                OrderAbnormal oa = new OrderAbnormal();
                oa.setType(orderAbnormalVO.getType());
                oa.setPunish(0);
                oa.setChargeUser(orderAbnormalVO.getChargeUser());
                oa.setOrderId(Long.valueOf(orderAbnormalVO.getOrderId()));
                oa.setOverTime(orderAbnormalVO.getOverTime());
                oa.setStatus(0);
                oa.setCreateUser(orderAbnormalVO.getCreateUser());
               // oa.setTransportId(orderAbnormalVO.getTransportId());
                oa.setTenantId(orderAbnormalVO.getTenantId());


                orderAbnormalMapper.insert(oa);
            }
        }
        // 异常订单解除
        OrderAbnormalDTO orderAbnormalDTO = new OrderAbnormalDTO();
        orderAbnormalDTO.setStatus(0);
        orderAbnormalDTO.setColumn("os.id");

        Page<PkgOrderAbnormal> orderAbnormalList = orderAbnormalMapper.getOrderAbnormalList(PageFactory.defaultPage(1,9999), orderAbnormalDTO,null);
        if (orderAbnormalList != null && orderAbnormalList.getRecords().size() > 0) {
            for (PkgOrderAbnormal orderAbnormal : orderAbnormalList.getRecords()) {
                if (orderAbnormal.getType() == OrderAbnormalTypeEnum.PICK.type) {
                    if (orderAbnormal.getOrderStatus() != OrderStatusEnum.NEW_ORDER.status) {
                        orderAbnormalMapper.relieve(orderAbnormal.getId(), 1, null);
                    }
                } else if (orderAbnormal.getType() == OrderAbnormalTypeEnum.VERIFY.type) {
                    if (orderAbnormal.getOrderStatus() != OrderStatusEnum.WAIT_PICK.status) {
                        orderAbnormalMapper.relieve(orderAbnormal.getId(), 1, null);
                    }
                } else if (orderAbnormal.getType() == OrderAbnormalTypeEnum.TRANSPORT.type) {
                    if (orderAbnormal.getOrderStatus() != OrderStatusEnum.NOT_DEPART_ORDER.status) {
                        orderAbnormalMapper.relieve(orderAbnormal.getId(), 1, null);
                    }
                } else if (orderAbnormal.getType() == OrderAbnormalTypeEnum.DELIVERY.type) {
                    if (orderAbnormal.getOrderStatus() != OrderStatusEnum.WAIT_TAKE.status) {
                        orderAbnormalMapper.relieve(orderAbnormal.getId(), 1, null);
                    }
                } else if (orderAbnormal.getType() == OrderAbnormalTypeEnum.TRANSFER.type) {
                    if (orderAbnormal.getOrderStatus() != OrderStatusEnum.TRANSFERRING_SIGN.status) {
                        orderAbnormalMapper.relieve(orderAbnormal.getId(), 1, null);
                    }
                }
            }
        }
        log.info("异常订单扫描任务执行完毕----------------------------------");
    }

    /**
     * 计算超时时间
     * @param value
     * @param addValue
     * @return
     */
    private Date dealOverTime(Long value, Long addValue) {
        Long result = value + addValue;
        return new Date(result);
    }


}
